<?php
/**
 * 活动
 *
 */

defined('IN_KG') or exit('Access Invalid!');

class batchControl extends BaseSellerControl {
    /**
     * 单个活动信息页
     */
    public function indexOp(){
        // 获取所有分类
        $goods_class = Model('goods_class')->getGoodsClassListAll();
        $gc_html = '';
        foreach ($goods_class as $key => $class) {
            $gc_html .= "<option value=\"$class[gc_id]\">$class[gc_name]</option>";
        }
        Tpl::output('goods_class_html', $gc_html);



        $action = empty($_GET['action']) ? 'comments' : $_GET['action'];
        Tpl::showpage('batch_'.$action);
        // echo "string";
    }

    public function get_goods_by_classOp(){
        $gc_id = $_GET['gc_id'];
        $goods = Model('goods')->getGoodsList(array('gc_id' => $gc_id), 'goods_id, goods_name, goods_price, goods_image', 'goods_commonid', '', 100);
        echo "<select>";
        foreach ($goods as $key => $good) {
            $good['goods_image'] = cthumb($good['goods_image'], 360, 1);
            echo "<option value=\"$good[goods_id]\" data-value='".json_encode($good)."'>$good[goods_name]</option>";
        }
        echo "</select>";
        // output_data(array('goods' => $goods));
    }

    public function saveOp(){
        $goods_id = $_POST['goods_id'];
        $evaluate_comment = $_POST['comment'];
        $geval_image = $_POST['geval_image'];
        $dateline = $_POST['dateline'];

        // $users = rkcache('rand_members');
        // 使用文件缓存，不存redis。
        $users = require_once BASE_DATA_PATH.DS.'cache'.DS.'rand_members.php';
        $index = mt_rand(0, 1308);
        $member_id = $users[$index]['uid'];
        $member_name = $users[$index]['username'];
        $dateline = $dateline.' '.mt_rand(8,23).':'.mt_rand(0,60).':'.mt_rand(0,60);

        // $goods = rkcache('goods_commonid');
        $goods_model = Model('goods');
        $goods = $goods_model->getGoodsIdByCommonId($goods_id);

        $goods_info = $goods_model->getGoodsDetail($goods[0]);
        // $goods_info = Model('goods')->select(array('goods_id' => $goods[0]));
        // print_r($goods[0]);
        // print_r($goods_info);
        // die();
        $goods_info = $goods_info['goods_info'];
        // 增加销量
        $new_order = array(
            'order_id' => 0,
            'goods_id' => intval($goods_info['goods_id']),
            'goods_name' => $goods_info['goods_name'],
            'goods_num' => 1,
            'goods_price' => $goods_info['goods_price'],
            'goods_image' => $goods_info['goods_image'],
            'store_id' => 1,
            'buyer_id' => $member_id,
            'goods_spec' => $goods_info['current_spec_name'],
            // 'goods_contractid' => $goods_info['goods_id'],
        );
        $res2 = Model()->table('order_goods')->insert($new_order);

        $evaluate_goods_info = array();
        $evaluate_goods_info['geval_orderid'] = 0;
        $evaluate_goods_info['geval_orderno'] = 0;
        $evaluate_goods_info['geval_ordergoodsid'] = $res2;
        $evaluate_goods_info['geval_goodsid'] = $goods_info['goods_id'];
        $evaluate_goods_info['geval_goodsname'] = $goods_info['goods_name'];
        $evaluate_goods_info['geval_goodsprice'] = $goods_info['goods_price'];
        $evaluate_goods_info['geval_goodsimage'] = $goods_info['goods_image'];
        $evaluate_goods_info['geval_scores'] = 5;
        $evaluate_goods_info['geval_content'] = $evaluate_comment;
        $evaluate_goods_info['geval_isanonymous'] = 1;
        $evaluate_goods_info['geval_addtime'] = strtotime($dateline);
        $evaluate_goods_info['geval_storeid'] = 1;
        $evaluate_goods_info['geval_storename'] = '好商城V5';
        $evaluate_goods_info['geval_frommemberid'] = $member_id;
        $evaluate_goods_info['geval_frommembername'] = $member_name;
        $evaluate_goods_info['geval_image'] = $geval_image;
        $evaluate_goods_info['geval_content_again'] = '';
        $evaluate_goods_info['geval_image_again'] = '';
        $evaluate_goods_info['geval_explain_again'] = '';

        // $goods_id = $goods_info['goods_id'];
        $count = Model('goods')->countSaleNumByOrder($goods_id, true);
        wkcache('sale_'.$goods_id, $count, 86400);

        $prefix = 'goods_evaluation';
        dcache($goods_id, $prefix);
        $goods_ids = Model('goods')->getGoodsIdByCommonId($goods_id, 1);
        Model('evaluate_goods')->getEvaluateGoodsInfoByGoodsCommonID($goods_id, $goods_ids);
        // $count = rkcache('sale_'.$goods_commonid);
        // if (!$count) {
        //     $count += 1;
        //     wkcache('sale_'.$goods_commonid, $count, 86400);
        // }

        // print_r();
        $res = Model('evaluate_goods')->addEvaluateGoods($evaluate_goods_info);

        foreach (explode(',', $evaluate_goods_info['geval_image']) as $key => $value) {
            $evaluate_goods_info['geval_images'][] = snsThumb($value, 1024);
        }
        $evaluate_goods_info['geval_addtime'] = $dateline;
        echo json_encode(array('evaluation' => $res, 'order' => $res2, 'evaluate' => $evaluate_goods_info, 'count' =>$count));die();
    }

    public function file_uploadOp(){
        $member_id = 1;
        $fildname = 'file_0';
        $file = $_FILES[$filename];
        // print_r($_FILES);die();
        $model = Model();
        $upload = new UploadFile();
        $upload_dir = ATTACH_MALBUM.DS.$member_id.DS;
    
        $upload->set('default_dir',$upload_dir.$upload->getSysSetPath());
        $thumb_width    = '240,1024';
        $thumb_height   = '2048,1024';
    
        $upload->set('max_size',C('image_max_filesize'));
        $upload->set('thumb_width', $thumb_width);
        $upload->set('thumb_height',$thumb_height);
        $upload->set('fprefix',$member_id);
        $upload->set('thumb_ext', '_240,_1024');
        $result = $upload->upfile($fildname);
        if (!$result){
            echo json_encode($upload->error);
        }
        $img_path = $upload->getSysSetPath().$upload->file_name;
        list($width, $height, $type, $attr) = getimagesize(BASE_UPLOAD_PATH.DS.ATTACH_MALBUM.DS.$member_id.DS.$img_path);
    
        $image = explode('.', $file["name"]);

        $model_sns_alumb = Model('sns_album');
        $ac_id = $model_sns_alumb->getSnsAlbumClassDefault($member_id);
        $insert = array();
        $insert['ap_name']      = $image['0'];
        $insert['ac_id']        = $ac_id;
        $insert['ap_cover']     = $img_path;
        $insert['ap_size']      = intval($file['size']);
        $insert['ap_spec']      = $width.'x'.$height;
        $insert['upload_time']  = time();
        $insert['member_id']    = $member_id;
        $result = $model->table('sns_albumpic')->insert($insert);
    
        $data = array();
        $data['file_id'] = $result;
        $data['fildname'] = $fildname;
        $data['file_name'] = $img_path;
        $data['origin_file_name'] = $file["name"];
        $data['file_url'] = snsThumb($img_path, 240);
        // output_data($data);
        // return $data;
        echo json_encode($data);
    }
}
